package com.example.demo.service.impl;

import com.example.demo.common.Result;
import com.example.demo.dto.CheciAddDTO;
import com.example.demo.dto.CheciGetDTO;
import com.example.demo.entity.Checi;
import com.example.demo.mapper.CheciMapper;
import com.example.demo.mapper.VehicleMapper;
import com.example.demo.service.CheciService;
import com.example.demo.vo.CheciVO;
import com.example.demo.vo.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheciServiceImpl implements CheciService {
    @Autowired
    private CheciMapper checiMapper;

    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public Result getCheciIds() {
        List<Integer> ids = checiMapper.selectAllCheciId();
        return Result.success(ids);
    }

    @Override
    public Result updateCheci(CheciAddDTO checiAddDTO) {
        Checi checi = new Checi();
        checi.setId(checiAddDTO.getId());
        checi.setVehicleId(checiAddDTO.getVehicleId());
        checi.setRouteId(checiAddDTO.getRouteId());
        checi.setStartTime(checiAddDTO.getStartTime());
        checi.setEndTime(checiAddDTO.getEndTime());
        //查询座位数
        List<Integer> seatList = vehicleMapper.selectSeatsById(checi.getVehicleId());
        checi.setLastSeats(seatList.get(0));
        int res = checiMapper.updateCheci(checi);
        if(res > 0){
            return Result.success();
        }else{
            return Result.fail();
        }
    }

    @Override
    public Result getCheci(CheciGetDTO checiGetDTO) {
//        System.out.println("getcheci:"+checiGetDTO);
        Checi checi = new Checi();
        checi.setId(checiGetDTO.getId());
        checi.setVehicleId(checiGetDTO.getVehicleId());
        List<CheciVO> checiVOList = checiMapper.selectCheci(checi);
        ResponseData responseData = new ResponseData();
        responseData.setList(checiVOList);
        responseData.setTotal(checiVOList.size());
        return Result.success(responseData);
    }

    @Override
    public Result addCheci(CheciAddDTO checiAddDTO) {
        Checi checi = new Checi();
        checi.setVehicleId(checiAddDTO.getVehicleId());
        checi.setRouteId(checiAddDTO.getRouteId());
        checi.setStartTime(checiAddDTO.getStartTime());
        checi.setEndTime(checiAddDTO.getEndTime());
        //查询座位数
        List<Integer> seatList = vehicleMapper.selectSeatsById(checi.getVehicleId());
        checi.setLastSeats(seatList.get(0));
        int res = checiMapper.insertCheci(checi);
        if(res > 0){
            return Result.success();
        }else{
            return Result.fail();
        }
    }
}
